#include "TileAction.h"
#include "Inventory.h"
#include "GFuns.h"
#include "Game.h"

TileAction::TileAction(Tile aTile, int aX, int aY, Item* aSpecialItem)
{
	object = (EObject)aTile.upper;
	x = aX;
	y = aY;
	locked = rand()%2;
	menu = nullptr;

	if(aSpecialItem != nullptr)
	{
		items.push_back(*aSpecialItem);
		locked = false;
	}
	if(object == O_CHEST)
	{
		name = "Chest";

		int itemsNumber = rand()%4;
		for(int i=0; i<itemsNumber; i++)
			items.push_back(Inventory::GenerateItem(ITEM_JEWELRY, randInt(JEWELRY_NONE+1,JEWELRY_END-1)));
	}
	if(object == O_BARREL)
	{
		name = "Barrel";

		int itemsNumber = 1;
		for(int i=0; i<itemsNumber; i++)
			items.push_back(Inventory::GenerateItem(ITEM_FOOD, randInt(FOOD_NONE+1,FOOD_END-1)));
	}
	if(object == O_MARKETSTAND)
	{
		name = "Stand";

		int itemsNumber = 1;
		for(int i=0; i<itemsNumber; i++)
			items.push_back(Inventory::GenerateItem(ITEM_FOOD, randInt(FOOD_NONE+1,FOOD_END-1)));
	}
	if(object == O_CRATE)
	{
		name = "Crate";

		int itemsNumber = rand()%6;
		for(int i=0; i<itemsNumber; i++)
			items.push_back(Inventory::GenerateItem(ITEM_DAILY, randInt(DAILY_NONE+1,DAILY_END-1)));
	}
	if(object == O_WARDROBE)
	{
		name = "Wardrobe";

		int itemsNumber = rand()%3;
		for(int i=0; i<itemsNumber; i++)
			items.push_back(Inventory::GenerateItem(ITEM_CLOTH, randInt(CLOTH_NONE+1,CLOTH_END-1)));
	}
	if(object == O_GRAVE)
	{
		name = "Grave";

		items.push_back(Inventory::GenerateItem(ITEM_QUEST, QUEST_GODS_TEARS));
	}
}

TileAction::~TileAction()
{
	delete menu;
}

void TileAction::RefreshItems()
{
	menu->menus.clear();
	for(unsigned int i=0; i<items.size(); i++)
	{
		MenuItem* item = menu->AddMenu(items[i].GetName());
		(item->AddMenu("Take"))->action = ACTION_ITEM_TAKE;
		item->AddMenu("Back");
	}
	menu->AddMenu("Back");
}

MenuItem* TileAction::GetMenu()
{
	if(object == O_CHEST || object == O_WARDROBE)
	{
		menu = new MenuItem("Open");
		menu->isOpen = false;

		if(locked)
		{
			menu->AddMenu("Locked");
		}
		else
			RefreshItems();
	}

	if(object == O_BARREL || object == O_CRATE || object == O_MARKETSTAND)
	{
		menu = new MenuItem("Look inside");
		menu->isOpen = false;

		RefreshItems();
	}

	if(object == O_GRAVE)
	{
		menu = new MenuItem("Examine");
		menu->isOpen = false;

		Item* item = new Item(ITEM_DAILY, DAILY_LITTLE_EMPTY_FLASK);
		if(!Game::GetHero()->HaveItem(item))
		{
			menu->AddMenu("A wet grave");
		}
		else
			RefreshItems();
	}

	return menu;
}
